package org.jsbd.boss.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@SuppressWarnings("all")
public class SortList<E> {
	
	public void Sort(List<E> list, final String method, final String sort) {
		Collections.sort(list, new Comparator() {
			public int compare(Object a, Object b) {
				int ret = 0;
				try {
					Method m1 = ((E) a).getClass().getMethod(method, null);
					Method m2 = ((E) b).getClass().getMethod(method, null);
					Object obj1 = m1.invoke(((E) a), null);
					Object obj2 = m2.invoke(((E) b), null);
					if (obj1 == null) {
						obj1 = new Object();
					}
					if (obj2 == null ) {
						obj2 = new Object();
					}
					if (obj1 != null && obj2 != null) {
						if (sort != null && "desc".equals(sort))
							ret = obj2.toString().compareTo(obj1.toString());// 倒序
						else
							ret = obj1.toString().compareTo(obj2.toString());// 正序
					} 
				} catch (NoSuchMethodException ne) {
					System.out.println(ne);
				} catch (IllegalAccessException ie) {
					System.out.println(ie);
				} catch (InvocationTargetException it) {
					System.out.println(it);
				}
				return ret;
			}
		});
	}
	
}
